[[...path]].page.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import React from 'react';
  2. import { IUserHasId } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { useTranslation } from 'next-i18next';
  7. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  8. import dynamic from 'next/dynamic';
  9. import Head from 'next/head';
  10. import { useCurrentGrowiLayoutFluidClassName } from '~/client/services/layout';
  11. import CountBadge from '~/components/Common/CountBadge';
  12. import PageListIcon from '~/components/Icons/PageListIcon';
  13. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  14. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  15. import { Page } from '~/components/Page';
  16. import styles from '~/components/Page/DisplaySwitcher.module.scss'; // for PageList toc style
  17. import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
  18. import TableOfContents from '~/components/TableOfContents';
  19. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  20. import { CrowiRequest } from '~/interfaces/crowi-request';
  21. import { RendererConfig } from '~/interfaces/services/renderer';
  22. import { IShareLinkHasId } from '~/interfaces/share-link';
  23. import {
  24. useCurrentUser, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  25. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useDrawioUri, useIsContainerFluid,
  26. } from '~/stores/context';
  27. import { useDescendantsPageListModal } from '~/stores/modal';
  28. import loggerFactory from '~/utils/logger';
  29. import { NextPageWithLayout } from '../_app.page';
  30. import {
  31. CommonProps, getServerSideCommonProps, generateCustomTitle, getNextI18NextConfig,
  32. } from '../utils/commons';
  33. const logger = loggerFactory('growi:next-page:share');
  34. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  35. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  36. type Props = CommonProps & {
  37. shareLink?: IShareLinkHasId,
  38. isExpired: boolean,
  39. disableLinkSharing: boolean,
  40. isSearchServiceConfigured: boolean,
  41. isSearchServiceReachable: boolean,
  42. isSearchScopeChildrenAsDefault: boolean,
  43. drawioUri: string | null,
  44. rendererConfig: RendererConfig,
  45. };
  46. const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
  47. useIsSearchPage(false);
  48. useShareLinkId(props.shareLink?._id);
  49. useCurrentPageId(props.shareLink?.relatedPage._id);
  50. useCurrentUser(props.currentUser);
  51. useCurrentPathname(props.currentPathname);
  52. useRendererConfig(props.rendererConfig);
  53. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  54. useIsSearchServiceReachable(props.isSearchServiceReachable);
  55. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  56. useDrawioUri(props.drawioUri);
  57. useIsContainerFluid(props.isContainerFluid);
  58. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  59. const { t } = useTranslation();
  60. const growiLayoutFluidClass = useCurrentGrowiLayoutFluidClassName();
  61. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  62. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  63. const shareLink = props.shareLink;
  64. const title = generateCustomTitle(props, 'GROWI');
  65. return (
  66. <>
  67. <Head>
  68. <title>{title}</title>
  69. </Head>
  70. <div className={`dynamic-layout-root ${growiLayoutFluidClass} h-100 d-flex flex-column justify-content-between`}>
  71. <header className="py-0 position-relative">
  72. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  73. </header>
  74. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  75. <div className="flex-grow-1">
  76. <div id="content-main" className="content-main grw-container-convertible">
  77. { props.disableLinkSharing && (
  78. <div className="mt-4">
  79. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  80. </div>
  81. )}
  82. { (isNotFound && !props.disableLinkSharing) && (
  83. <div className="container-lg">
  84. <h2 className="text-muted mt-4">
  85. <i className="icon-ban" aria-hidden="true" />
  86. <span> Page is not found</span>
  87. </h2>
  88. </div>
  89. )}
  90. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  91. <div className="container-lg">
  92. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  93. <h2 className="text-muted mt-4">
  94. <i className="icon-ban" aria-hidden="true" />
  95. <span> Page is expired</span>
  96. </h2>
  97. </div>
  98. )}
  99. {(isShowSharedPage && shareLink != null) && (
  100. <>
  101. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  102. <div className="d-flex flex-column flex-lg-row-reverse">
  103. <div className="grw-side-contents-container">
  104. <div className="grw-side-contents-sticky-container">
  105. {/* Page list */}
  106. <div className={`grw-page-accessories-control ${styles['grw-page-accessories-control']}`}>
  107. { shareLink.relatedPage.path != null && (
  108. <button
  109. type="button"
  110. className="btn btn-block btn-outline-secondary grw-btn-page-accessories
  111. rounded-pill d-flex justify-content-between align-items-center"
  112. onClick={() => openDescendantPageListModal(shareLink.relatedPage.path)}
  113. data-testid="pageListButton"
  114. >
  115. <div className="grw-page-accessories-control-icon">
  116. <PageListIcon />
  117. </div>
  118. {t('page_list')}
  119. <CountBadge count={shareLink.relatedPage.descendantCount} offset={1} />
  120. </button>
  121. ) }
  122. </div>
  123. <div className="d-none d-lg-block">
  124. <TableOfContents />
  125. </div>
  126. </div>
  127. </div>
  128. <div className="flex-grow-1 flex-basis-0 mw-0">
  129. <Page />
  130. </div>
  131. </div>
  132. </>
  133. )}
  134. </div>
  135. </div>
  136. </div>
  137. </>
  138. );
  139. };
  140. SharedPage.getLayout = function getLayout(page) {
  141. return (
  142. <>
  143. <DrawioViewerScript />
  144. <ShareLinkLayout>{page}</ShareLinkLayout>
  145. </>
  146. );
  147. };
  148. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  149. const req: CrowiRequest = context.req as CrowiRequest;
  150. const { crowi } = req;
  151. const { configManager, searchService, xssService } = crowi;
  152. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  153. props.isSearchServiceConfigured = searchService.isConfigured;
  154. props.isSearchServiceReachable = searchService.isReachable;
  155. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  156. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  157. props.rendererConfig = {
  158. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  159. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  160. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  161. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  162. plantumlUri: process.env.PLANTUML_URI ?? null,
  163. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  164. // XSS Options
  165. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  166. attrWhiteList: xssService.getAttrWhiteList(),
  167. tagWhiteList: xssService.getTagWhiteList(),
  168. highlightJsStyleBorder: configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  169. };
  170. }
  171. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  172. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  173. props._nextI18Next = nextI18NextConfig._nextI18Next;
  174. }
  175. function getAction(props: Props): SupportedActionType {
  176. let action: SupportedActionType;
  177. if (props.isExpired) {
  178. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  179. }
  180. else if (props.shareLink == null) {
  181. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  182. }
  183. else {
  184. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  185. }
  186. return action;
  187. }
  188. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  189. const req: CrowiRequest = context.req as CrowiRequest;
  190. const parameters = {
  191. ip: req.ip,
  192. endpoint: req.originalUrl,
  193. action,
  194. user: req.user?._id,
  195. snapshot: {
  196. username: req.user?.username,
  197. },
  198. };
  199. await req.crowi.activityService.createActivity(parameters);
  200. }
  201. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  202. const req = context.req as CrowiRequest<IUserHasId & any>;
  203. const { crowi, params } = req;
  204. const result = await getServerSideCommonProps(context);
  205. if (!('props' in result)) {
  206. throw new Error('invalid getSSP result');
  207. }
  208. const props: Props = result.props as Props;
  209. try {
  210. const ShareLinkModel = crowi.model('ShareLink');
  211. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  212. if (shareLink != null) {
  213. props.isExpired = shareLink.isExpired();
  214. props.shareLink = shareLink.toObject();
  215. }
  216. }
  217. catch (err) {
  218. logger.error(err);
  219. }
  220. injectServerConfigurations(context, props);
  221. await injectNextI18NextConfigurations(context, props);
  222. await addActivity(context, getAction(props));
  223. return {
  224. props,
  225. };
  226. };
  227. export default SharedPage;